package com.hyl.accountbook;

import com.hyl.dao.DBOpenHelper;
import com.hyl.net.DataSaveUtil.ListDataSave;
import com.hyl.net.api.HttpClient;
import com.hyl.net.network.GetRegister_Interface;
import com.hyl.net.network_data.recv_data.Register_body;
import com.hyl.net.network_data.send_data.Register;
import com.hyl.util.pubFun;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class RegistActivity extends AppCompatActivity {
    private EditText editPhone;
    private EditText editPwd;
    private Button btnRegist;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regist);
        editPhone = (EditText) findViewById(R.id.editPhone);
        editPwd = (EditText) findViewById(R.id.editPwd);
        btnRegist = (Button) findViewById(R.id.btnRegist);
    }

    /**
     * register event
     * @param v
     */
    public void OnMyRegistClick(View v)
    {
        boolean isTrue = true;
//        if(pubFun.isPhoneNumberValid(editPhone.getText().toString()) == false){
//            isTrue = false;
//            Toast.makeText(this, "手机号格式不正确！", Toast.LENGTH_SHORT).show();
//            return;
//        }
        if(pubFun.isEmpty(editPwd.getText().toString())){
            isTrue = false;
            Toast.makeText(this, "密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        if(isTrue = true){
            //call DBOpenHelper
            DBOpenHelper helper = new DBOpenHelper(this,"qianbao.db",null,1);
            SQLiteDatabase db = helper.getWritableDatabase();
            Cursor c = db.query("user_tb",null,"userID=?",new String[]{editPhone.getText().toString()},null,null,null);
            if(c!=null && c.getCount() >= 1){
                Toast.makeText(this, "该用户已存在", Toast.LENGTH_SHORT).show();
                c.close();
            }
            else{
                //insert data
                ContentValues values= new ContentValues();
                values.put("userID",editPhone.getText().toString());
                values.put("pwd",editPwd.getText().toString());

//---------------------------------------------------------网络链接---------------
                // TODO validate success, do something
                // 在这里通过网络请求将用户名和密码的信息发送给后端，如果验证正确，则跳转到登陆页面，否则提示用户账号和密码错误
                // 创建 网络请求接口 的实例,范型传入返回的结果
                HttpClient<Register_body> httpClient = HttpClient.getInstance();

                // 请求体传入接收的结果
                GetRegister_Interface request = httpClient.getHttpClient().create(GetRegister_Interface.class);
                //对 发送请求 进行封装
                Call<Register_body> call = request.getCall(new Register(Integer.parseInt(editPhone.getText().toString()),editPwd.getText().toString()));
                httpClient.start(call, new Callback<Register_body>() {
                    //请求成功时回调
                    @Override
                    public void onResponse(Call<Register_body> call, final Response<Register_body> response) {

                        if (response.body() == null) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(RegistActivity.this, "请求出错", Toast.LENGTH_SHORT).show();
                                }
                            });
                            return;
                        }

                        // 等于200 说明请求成功，跳转页面
                        if (response.body().getStatus() == 200) {

//                            // 登陆成功，同时把注册的数据保存到本地
//                            ListDataSave.getInstance(mContext,"Userdata").setData("user",response.body().getData());

                            // 进行跳转
                            Intent intent = new Intent(RegistActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        } else {

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(RegistActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            });

                        }

                    }
                    //请求失败时候的回调
                    @Override
                    public void onFailure(Call<Register_body> call, Throwable throwable) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(RegistActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

//----------------------------------网络链接----------------------------------------------
                long rowid = db.insert("user_tb",null,values);
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                this.finish();
            }
            db.close();
        }else{
            return;
        }
    }
}
